package com.neusoft.neuiotms.dcvm.repository.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcvm.model.Mock;
import com.neusoft.neuiotms.dcvm.model.QMock;
import com.neusoft.neuiotms.dcvm.repository.CustomizedMockRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedMockRepositoryImpl extends AbstractRepository implements CustomizedMockRepository {

    @Override
    public Page<Mock> search(Pageable pageable, Searchable searchable) {
        final QMock qMock = QMock.mock;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Mock> query = this.query().selectFrom(qMock);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(Searchable searchable) {
        final QMock qMock = QMock.mock;
        final BooleanBuilder where = new BooleanBuilder();
        where.or(super.containsStr(qMock.api, searchable, "kw"));
        where.or(super.containsStr(qMock.description, searchable, "kw"));
        return where;
    }

    @Override
    protected Class<?> getModelClass() {
        return Mock.class;
    }

}
